namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Web;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Web;
    using Xunit;

    public sealed class SiteFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Site>()
                .DerivesFrom<Resource>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("site")
                .Implements<IXmlSerializable>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Site actual = (
                "<site host='example.com' machine='ABC123'>" +
                    "<assemblies>" +
                        "<assembly>Example</assembly>" +
                    "</assemblies>" +
                "</site>").Deserialize<Site>();

            Assert.Equal<string>("Example", actual.Assemblies[0]);
            Assert.Equal<string>("example.com", actual.Host);
            Assert.Equal<string>("ABC123", actual.Machine);
        }

        [Fact]
        public void deserializeEmpty()
        {
            Site actual = "<site />".Deserialize<Site>();

            Assert.NotNull(actual.Assemblies);
            Assert.Null(actual.Host);
            Assert.Null(actual.Machine);
        }

        [Fact]
        public void serialize()
        {
            Site obj = new Site
            {
                Host = "example.com",
                Machine = "ABC123",
                Assemblies = new Collection<string>
                {
                    "Example"
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("'Example'=/site[@host='example.com'][@machine='ABC123']/assemblies/assembly"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Site());
        }

        [Fact]
        public void prop_Information_get()
        {
            try
            {
                string host = "example.com";
                string machine = "ABC123";

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<NameValueCollection>(x => x.Request.Headers).Returns(new NameValueCollection { { "Host", host } }).Verifiable();
                mock.SetupGet<string>(x => x.Server.MachineName).Returns(machine).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Site obj = Site.Information;

                Assert.NotEmpty(obj.Assemblies);
                Assert.Equal<string>(host, obj.Host);
                Assert.Equal<string>(machine, obj.Machine);
                Assert.Equal<string>("Site Information", obj.Title);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Cacheability_get()
        {
            Assert.Equal<HttpCacheability>(HttpCacheability.NoCache, new Site().Cacheability.Value);
        }

        [Fact]
        public void prop_Title_get()
        {
            Assert.Equal<string>("Site Information", new Site().Title);
        }

        [Fact]
        public void prop_Assemblies()
        {
            Assert.True(new PropertyExpectations(typeof(Site).GetProperty("Assemblies"))
                .TypeIs<Collection<string>>()
                .DefaultValueIsNotNull()
                .JsonProperty()
                .XmlArray("assemblies", "assembly")
                .Result);
        }

        [Fact]
        public void prop_Host()
        {
            Assert.True(new PropertyExpectations(typeof(Site).GetProperty("Host"))
                .IsAutoProperty<string>()
                .JsonProperty()
                .XmlAttribute("host")
                .Result);
        }

        [Fact]
        public void prop_Machine()
        {
            Assert.True(new PropertyExpectations(typeof(Site).GetProperty("Machine"))
                .IsAutoProperty<string>()
                .JsonProperty()
                .XmlAttribute("machine")
                .Result);
        }

        [Fact]
        public void op_ToString()
        {
            Site obj = new Site
            {
                Host = "example.com",
                Machine = "ABC123",
                Assemblies = new Collection<string>
                {
                    "Example"
                }
            };

            string expected =
                "[Server]" + Environment.NewLine +
                "Host: example.com" + Environment.NewLine +
                "Machine: ABC123" + Environment.NewLine +
                Environment.NewLine +
                "[Assemblies]" + Environment.NewLine +
                "Assembly: Example" + Environment.NewLine;
            string actual = obj.ToString();

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new Site() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new Site() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new Site() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}